﻿using System.Text;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;

namespace UTL
{
    public class BaseUTL
    {
        public static bool ExeSQL(string sqlConnectionString)
        {
            var rdr = new StreamReader(@"xDOMv1.sql");
            var sql = rdr.ReadToEnd();
            
            var builder = new SqlConnectionStringBuilder(sqlConnectionString) { InitialCatalog = "master" };
            using (var sqlConnection = new SqlConnection(builder.ToString()))
            {
                var serverConnection = new ServerConnection(sqlConnection);
                var server = new Server(serverConnection);
                server.ConnectionContext.ExecuteNonQuery(sql); // hangs here for about 12 -15 seconds
            }

            return true;
        }

        public static string PrintInfo()
        {
            var svr = new Server();
            string sqlConnectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
            ExeSQL(sqlConnectionString);
            return String.Format("{0}\n{1}", svr.Name, svr.Information.VersionString);
        }
    }
}